package com.cx.algrithm.leetcode.day1;

/**
 * @Author: robert.song
 * @Date: Created in 2021/12/22
 */

import java.util.Stack;

/**
 * 定义栈结构，在该类型中实现一个可以得到栈的最小元素的min函数，调用min push pop的时间复杂度都是 O(1)
 */
public class MinValue {
    static Stack<Integer> A = new Stack<>();
    static Stack<Integer> B = new Stack<>();

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            push(i);
        }
        System.out.println(min());

        System.out.println(A);
        System.out.println(B);
        System.out.println(top());
        System.out.println(min());
    }

    public static void push(int x) {
        A.add(x);
        if (B.empty() || B.peek() >= x) {
            B.add(x);
        }
    }

    public static void pop() {
        if (A.pop().equals(B.peek())) {
            B.pop();
        }
    }

    public static int top() {
        return A.peek();
    }

    public static int min() {
        return B.peek();
    }
}
